/**
 * Gome SN Generator 
 */
package cn.com.gome.sn.inapi.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.gome.sn.inapi.base.AdvanceApiBaseController;
import cn.com.gome.sn.inapi.service.ServiceAService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.com.gome.frame.logger.HLogger;
import cn.com.gome.frame.logger.LoggerManager;
import cn.com.gome.sn.inapi.utils.APPAESUtils;
import cn.com.gome.sn.inapi.utils.JsonTools;


/**
 * 金融业务
 */
@Controller
@RequestMapping("/test")
public class TestApi extends AdvanceApiBaseController {

    protected HLogger logger = LoggerManager.getLoger("TestApi");

    public final String charset = "UTF-8";

    @Value("#{inapi['md5Key']}")
    private String md5Key;

    @Value("#{inapi['aesKey']}")
    private String aesKey;

    @Autowired
    private ServiceAService serviceAService;

    /**
     * 具有加密验签功能的api接口
     * @param request
     * @param response
     * @param requestjson
     * @return
     */
    @RequestMapping(value = "test.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String test(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestjson) {
        logger.info("TestApi.test--start");
        logger.info("requestjson:" + requestjson);
        // 返回值
        String responsejson = "";
        String reqMessageId = "";
        try {
            Map<String, Object> paramMap = JSONObject.fromObject(requestjson);
            reqMessageId = (String)paramMap.get("reqMessageId");//请求ID
            String reqTime      = (String)paramMap.get("reqTime");//请求时间
            String version      = (String)paramMap.get("version");//版本号
            String systemNo     = (String)paramMap.get("systemNo");//系统编号
            String encReqInfo   = (String)paramMap.get("encReqInfo");//加密请求信息
            String reqType      = (String)paramMap.get("reqType");//交易类型
            String sign         = (String)paramMap.get("sign");//签名信息
            //请求参数校验
            if(!checkParamMap(paramMap)){
                responsejson=returnMsg(RESULT_FAIL,"请求参数不全",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            }
            //MD5验签
            if(!validateMd5(systemNo, reqTime, encReqInfo, sign, md5Key)){
                responsejson=returnMsg(RESULT_FAIL,"验签失败",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            }
            String reqJosn = APPAESUtils.decrypt(encReqInfo, aesKey);
            logger.info("接收inapi接口请求数据" + ",reqTime:" + reqTime + " reqJosn:" + reqJosn);
            Map<String, Object> reqMap = JsonTools.parseJSON2Map(reqJosn);
            reqMap.put("reqMessageId", reqMessageId);
            //业务逻辑处理
            Map<String, Object> retMap = null;
            if("10000000".equals(reqType)) {
                responsejson=returnMsg(RESULT_SUCCESS,"接口联通测试成功",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            } else if("10000001".equals(reqType)) {
                retMap = serviceAService.exec(reqMap);
            } else {
                responsejson=returnMsg(RESULT_FAIL,"没有找到对应的reqType",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            }
            //RESCODE和RESDESC由SERVICE内确定
            responsejson = returnMsg(retMap, reqMessageId);
        } catch (Exception e) {
            logger.error("接口处理异常", e);
            responsejson=returnMsg(RESULT_FAIL,"接口处理异常",null,reqMessageId);
            logger.info("responsejson:" + responsejson);
            return responsejson;
        }
        logger.info("TestApi.test--end");
        return responsejson;
    }
    /**
     * 不具有加密验签功能的api接口
     * @param request
     * @param response
     * @param requestjson
     * @return
     */
    @RequestMapping(value = "testne.do", produces = {"application/json;charset=UTF-8"})
    public String testne(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestjson) {
        logger.info("TestApine.test--start");
        logger.info("requestjson:" + requestjson);
        // 返回值
        String responsejson = "";
        String reqMessageId = "";
        try {
            Map<String, Object> paramMap = JSONObject.fromObject(requestjson);
            reqMessageId = (String)paramMap.get("reqMessageId");//请求ID
            String reqTime      = (String)paramMap.get("reqTime");//请求时间
            String version      = (String)paramMap.get("version");//版本号
            String systemNo     = (String)paramMap.get("systemNo");//系统编号
            String reqType      = (String)paramMap.get("reqType");//交易类型
            //请求参数校验
            if(!checkParamMapne(paramMap)){
                responsejson=returnMsg(RESULT_FAIL,"请求参数不全",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            }
            logger.info("接收inapi接口请求数据" + ": paramMap:" + paramMap);
            Map<String, Object> reqMap = paramMap;
            //业务逻辑处理
            Map<String, Object> retMap = null;
            if("10000000".equals(reqType)) {
                responsejson=returnMsg(RESULT_SUCCESS,"接口联通测试成功",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            } else if("10000001".equals(reqType)) {
                retMap = serviceAService.exec(reqMap);
            } else {
                responsejson=returnMsg(RESULT_FAIL,"没有找到对应的reqType",null,reqMessageId);
                logger.info("responsejson:" + responsejson);
                return responsejson;
            }
            //RESCODE和RESDESC由SERVICE内确定
            responsejson = returnMsg(retMap, reqMessageId);
        } catch (Exception e) {
            logger.error("接口处理异常", e);
            responsejson=returnMsg(RESULT_FAIL,"接口处理异常",null,reqMessageId);
            logger.info("responsejson:" + responsejson);
            return responsejson;
        }
        logger.info("TestApi.test--end");
        return responsejson;
    }
}

